<?php
/**
 * 合作商列表
 **/
include "../includes/common.php";
$title = '合作商列表';
include './head.php';
if ($islogin == 1) {} else {
    exit("<script language='javascript'>window.location.href='./login.php';</script>");
}
?>
<div class="container" style="padding-top:70px;">
    <div class="col-md-12 center-block" style="float: none;">
        <form onsubmit="return searchSubmit()" method="GET" class="form-inline" id="searchToolbar">
            <input type="hidden" class="form-control" name="gid">
            <input type="hidden" class="form-control" name="upid">
            <div class="form-group">
                <label>搜索</label>
                <select name="column" class="form-control">
                    <option value="id">合作商编号</option>
                    <option value="name">合作商名称</option>
                    <option value="sign_key">密钥</option>
                    <option value="domain">域名</option>
                    <option value="qq">QQ</option>
                    <option value="tel">手机号码</option>
                    <option value="email">邮箱</option></select>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="value" placeholder="搜索内容">
            </div>
            <div class="form-group">
                <select name="dstatus" class="form-control">
                    <option value="0">全部用户</option>
                    <option value="status_1">状态正常</option>
                    <option value="status_0">状态关闭</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">搜索</button>&nbsp;<a href="./partnerset.php?my=add" class="btn btn-success">添加合作商</a>
            <a href="javascript:searchClear()" class="btn btn-default" title="刷新用户列表"><i class="fa fa-refresh"></i></a>
        </form>
        <table id="listTable"></table>
    </div>
</div>
<script src="<?php echo $cdnpublic ?>layer/3.1.1/layer.js"></script>
<script src="<?php echo $cdnpublic ?>clipboard.js/1.7.1/clipboard.min.js"></script>
<script src="../assets/js/bootstrap-table.min.js"></script>
<script src="../assets/js/bootstrap-table-page-jump-to.min.js"></script>
<script src="../assets/js/custom.js"></script>
<script>
var pay_domain = '<?php echo ($conf['pay_domain_forbid'] == 1 || $conf['pay_domain_open'] == 1) ? 'true' : 'false'; ?>';
$(document).ready(function(){
	updateToolbar();
	const defaultPageSize = 30;
	const pageNumber = typeof window.$_GET['pageNumber'] != 'undefined' ? parseInt(window.$_GET['pageNumber']) : 1;
	const pageSize = typeof window.$_GET['pageSize'] != 'undefined' ? parseInt(window.$_GET['pageSize']) : defaultPageSize;

	$("#listTable").bootstrapTable({
		url: 'ajax_partner.php?act=userList',
		pageNumber: pageNumber,
		pageSize: pageSize,
		classes: 'table table-striped table-hover table-bordered',
		columns: [
			{
				field: 'uid',
				title: '合作商编号/名称',
				formatter: function(value, row, index) {
					return '<b>'+row.id+'</b>[<a href="javascript:showKey('+row.id+',\''+row.sign_key+'\')">查看密钥</a>]<br/><span>'+row.name+'</span>';
				}
			},
			{
				field: 'qq',
				title: '联系方式',
				formatter: function(value, row, index) {
					return (value ? 'QQ:'+(isMobile() ? '<a href="mqqwpa://im/chat?chat_type=wpa&version=1&src_type=web&web_src=oicqzone.com&uin='+value+'">'+value+'</a>' : '<a href="tencent://message/?uin='+value+'&amp;Site=qq&amp;Menu=yes">'+value+'</a>') : '')+'<br/>'+(row.tel?row.tel:'')+'<br/>'+(row.email?row.email:'');
				}
			},
			{
				field: 'domain',
				title: '域名/添加时间',
				formatter: function(value, row, index) {
					return (value?value:'')+(pay_domain=='true'?' [<a href="'+row.domain+'" target="_blank" >域名</a>]':'')+'<br/>'+row.create_time;
				}
			},
			{
				field: 'status',
				title: '状态',
				formatter: function(value, row, index) {
					let html = '';
					if(value == '1'){
						html += '<a href="javascript:setStatus('+row.id+',0)"><font color=green><i class="fa fa-check-circle"></i>正常</font></a>';
					}else{
						html += '<a href="javascript:setStatus('+row.id+',1)"><font color=red><i class="fa fa-times-circle"></i>封禁</font></a>';
					}
					return html;
				}
			},
			{
				field: '',
				title: '操作',
				formatter: function(value, row, index) {
					return '<a href="./partnerset.php?my=edit&id='+row.id+'" class="btn btn-xs btn-info">编辑</a>&nbsp;<a href="javascript:delUser('+row.id+')" class="btn btn-xs btn-danger">删除</a>';
				}
			},
		],
	})
})

function showKey(uid,key){
	var clipboard;
	var confirmobj = layer.confirm(key+'<input type="hidden" id="copyContent" value="'+key+'"/>', {
	  title:'查看密钥',shadeClose:true,btn: ['复制','重置','关闭'], success: function(){
		clipboard = new Clipboard('.layui-layer-btn0',{text: function() {return key;}});
		clipboard.on('success', function (e) {
			$(".layui-layer-btn0").text('复制成功')
		});
		clipboard.on('error', function (e) {
			$(".layui-layer-btn0").text('复制失败')
		});
	  }
	  ,end: function(){
		clipboard.destroy();
	  }
	}, function(){}, function(){
		if(!confirm('确定要重置商户'+uid+'的密钥吗？')){
			showKey(uid,key);return;
		}
		$.ajax({
			type : 'GET',
			url : 'ajax_partner.php?act=resetUser&id='+uid,
			dataType : 'json',
			success : function(data) {
				if(data.code == 0){
					alert('重置密钥成功！');
					showKey(uid,data.key);
                    searchClear();
				}else{
					layer.alert(data.msg, {icon:2});
				}
			},
			error:function(data){
				layer.msg('服务器错误');
				return false;
			}
		});
	}, function(){
		layer.close(confirmobj);
	});
}
function setStatus(uid,status) {
	$.ajax({
		type : 'POST',
		url : 'ajax_partner.php?act=setUser',
		data : {id:uid, status:status},
		dataType : 'json',
		success : function(data) {
			if(data.code == 0){
				searchSubmit();
			}else{
				layer.msg(data.msg, {icon:2, time:1500});
			}
		},
		error:function(data){
			layer.msg('服务器错误');
			return false;
		}
	});
}

function delUser(uid) {
	var confirmobj = layer.confirm('你确实要删除此商户吗？', {
	  btn: ['确定','取消'], icon:0
	}, function(){
	  var ii = layer.load(2, {shade:[0.1,'#fff']});
	  $.ajax({
		type : 'GET',
		url : 'ajax_partner.php?act=delUser&id='+uid,
		dataType : 'json',
		success : function(data) {
			layer.close(ii);
			if(data.code == 0){
				layer.closeAll();
				searchSubmit();
			}else{
				layer.alert(data.msg, {icon: 2});
			}
		},
		error:function(data){
			layer.close(ii);
			layer.msg('服务器错误');
		}
	  });
	}, function(){
	  layer.close(confirmobj);
	});
}

</script>